﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using TouchRC.MetadataCaching.Extensions;

namespace MediaScoutGUI.GUITypes
{
    public class Episode : INotifyPropertyChanged
    {
        private String name;
        private FileInfo m_Filepath;
        private String epname;
        public Season Parent;
        private FileInfo poster = null;
        public String Id;
        private String description;
        private int m_EpisodeNumber;

        public FileInfo MetadataPath
        {
            get
            {
                DirectoryInfo dir = m_Filepath.Directory;
                String strippedname = m_Filepath.GetFileNameWithoutExtension();

                DirectoryInfo metadataFolder = new DirectoryInfo(Path.Combine(dir.FullName, @"metadata"));

                FileInfo metadataFile = new FileInfo(Path.Combine(metadataFolder.FullName, strippedname + ".xml"));
                return metadataFile;
            }
        }

        public String Description
        {
            get { return description; }
            set { description = value; }
        }

        private Boolean hasMetadata;

        public Boolean HasMetadata
        {
            get { return hasMetadata; }
            set { hasMetadata = value; }
        }


        public FileInfo Poster
        {
            get { return poster; }
            set { poster = value; }
        }

        public String Epname
        {
            get { return epname; }
            set { epname = value; }
        }

        public Episode()
        {

        }

        public Episode(FileInfo xmlPath)
        {
            LoadFromXML(xmlPath);
        }

        public Episode(FileInfo filepath, Season Parent)
        {
            this.Parent = Parent;
            m_Filepath = filepath;
            this.name = filepath.Name;

            MetadataCheck();
            LoadFromXML(MetadataPath);
        }

        public void MetadataCheck()
        {
            if (File.Exists(MetadataPath.FullName))
                HasMetadata = true;

            NotifyPropertyChanged("HasMetadata");
        }

        public void LoadFromXML(FileInfo xmlFile)
        {
            try
            {
                if (File.Exists(xmlFile.FullName))
                {
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.Load(xmlFile.FullName);
                    XmlNode node = xdoc.DocumentElement;

                    Id = node.SelectSingleNode("EpisodeID").InnerText;
                    m_EpisodeNumber = Convert.ToInt32(node.SelectSingleNode("ID").InnerText);
                    description = node.SelectSingleNode("Overview").InnerText;
                    epname = node.SelectSingleNode("EpisodeName").InnerText;
                    poster = new FileInfo(node.SelectSingleNode("filename").InnerText);
                    poster = new FileInfo(Path.Combine(MetadataPath.Directory.FullName, poster.Name));
                }
            }
            catch
            {
            }
        }

        public FileInfo Filepath
        {
            get { return m_Filepath; }
            set { m_Filepath = value; }
        }

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public int EpisodeNumber
        {
            get { return m_EpisodeNumber; }
            set { m_EpisodeNumber = value; }
        }

        public void UpdatePoster()
        {
            if (File.Exists(MetadataPath.FullName))
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(MetadataPath.FullName);
                XmlNode node = xdoc.DocumentElement;

                node.SelectSingleNode("filename").InnerText = poster.Name;
                xdoc.Save(MetadataPath.FullName);
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public override String ToString()
        {
            return this.Name;
        }
    }
}